package com.springboot.demo.controller;

import com.springboot.demo.domain.Dictionary;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("demo")
public class DemoController {

    /* 第一类：请求路径参数
      1、@PathVariable  获取路径参数。即url/{id}这种形式。
      2、@RequestParam  获取查询参数。即url?name=这种形式*/
    @GetMapping("/{id}")
    public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);
    }

    /* 第二类：Body参数
      1、@RequestBody raw */
    @PostMapping(path = "/demo1")
    public void demo1(@RequestBody Dictionary dictionary) {
        System.out.println(dictionary.toString());
    }

    @PostMapping(path = "/demo1/2")
    public void demo1(@RequestBody Map<String, String> dictionary) {
        System.out.println(dictionary.get("name"));
    }

    // 2、无注解 form-data
    @PostMapping(path = "/demo2")
    public void demo2(@Valid Dictionary dictionary) {
        System.out.println(dictionary.toString());
    }

    /* 第三类：请求头参数以及Cookie
     1、@RequestHeader
     2、@CookieValue */
    @GetMapping("/demo3")
    public void demo3(@RequestHeader(name = "myHeader") String myHeader,
                      @CookieValue(name = "myCookie") String myCookie) {
        System.out.println("myHeader=" + myHeader);
        System.out.println("myCookie=" + myCookie);
    }

    @GetMapping("/demo3/2")
    public void demo3(HttpServletRequest request) {
        System.out.println(request.getHeader("myHeader"));
        for (Cookie cookie : request.getCookies()) {
            if ("myCookie".equals(cookie.getName())) {
                System.out.println(cookie.getValue());
            }
        }
    }
}
